package com.vip.workflow.controller;

import com.vip.common.exception.BusinessException;
import com.vip.common.response.ErrorCode;
import com.vip.common.response.RestResponse;
import com.vip.common.response.RestResponses;
import com.vip.workflow.constant.UrlConsts;
import com.vip.workflow.dto.TaskDTO;
import com.vip.workflow.service.IFlowTaskService;
import com.vip.workflow.util.InputStreamUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

/**
 * 任务管理 Controller
 *
 * @author wgb
 * @date 2020/5/8 9:26
 */
@RestController
@RequestMapping(value = UrlConsts.WORK_FLOW + "/task")
public class TaskController {

    @Autowired
    private IFlowTaskService flowTaskService;

    /**
     * 获取待办任务列表
     *
     * @return
     */
    @GetMapping("/list")
    public RestResponse<List<TaskDTO>> getBackLogTask() throws Exception {
        if( true){
            throw new BusinessException(ErrorCode.WORKFLOW_ERROR, "获取待办任务列表错误");
        }
        return RestResponses.newResponseFromResult(flowTaskService.getBackLogTask());
    }

    /**
     * 启动工作流(创建新的任务)
     *
     * @param processDefinitionId  流程ID
     * @param businessId 业务ID
     * @param vars       参数
     * @return 任务ID
     */
    @PostMapping("/start/{processDefinitionId}")
    public RestResponse<String> startWorkFlow(@PathVariable String processDefinitionId, @RequestParam(required = false) String businessId
          , @RequestParam(required = false) String userId, @RequestBody(required = false) Map<String, Object> vars) {
        return RestResponses.newResponseFromResult(flowTaskService.startWorkFlow(processDefinitionId, businessId, userId, vars));
    }

    /**
     * 完成当前节点任务
     *
     * @param taskId
     * @param vars
     * @return
     */
    @PutMapping("/complete/{taskId}")
    public RestResponse<String> completeCurrTask(@PathVariable String taskId, @RequestBody(required = false) Map<String, Object> vars) {
        return RestResponses.newResponseFromResult(flowTaskService.completeCurrTask(taskId, vars));
    }

    /**
     * 关闭指定任务
     *
     * @param taskId
     * @return
     */
    @DeleteMapping("/close/{taskId}")
    public RestResponse<String> closeCurrTask(@PathVariable String taskId) {
        return RestResponses.newResponseFromResult(flowTaskService.closeCurrTask(taskId));
    }

    /**
     * 批量关闭任务
     *
     * @param taskIds
     * @return
     */
    @DeleteMapping("/close/multi")
    public RestResponse<String> closeTaskMulti(@RequestBody List<String> taskIds) {
        return RestResponses.newResponseFromResult(flowTaskService.closeTaskMulti(taskIds));
    }

    /**
     * 读取任务的跟踪图片
     *
     * @param taskId   任务ID
     */
    @GetMapping(value = "/photo/{taskId}", produces = MediaType.IMAGE_PNG_VALUE)
    public ResponseEntity<byte[]> getTaskTrackPhoto(@PathVariable String taskId) throws IOException {
        InputStream inputStream = flowTaskService.getTaskTrackPhoto(taskId);
        return new ResponseEntity<>(InputStreamUtils.inputStreamToByte(inputStream), HttpStatus.OK);
    }
}
